package com.ds.infra.ibatis.config;

import com.ds.infra.ibatis.transaction.interceptor.TransactionInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionAttributeSource;

import javax.sql.DataSource;

// @Configuration
@RequiredArgsConstructor
@EnableAspectJAutoProxy // 新增此注解
public class TransactionConfig {

    private final DataSource dataSource;

    // @Bean
    // public JdbcTransactionFactory transactionFactory() {
    //     return new JdbcTransactionFactory(dataSource);
    // }
    //
    // @Bean
    // public TransactionInterceptor transactionInterceptor() {
    //     return new TransactionInterceptor(dataSource);
    // }
    //
    // @Bean
    // public Advisor transactionAdvisor() {
    //     AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    //     pointcut.setExpression("@annotation(com.ds.infra.ibatis.transaction.annotation.Transactional)");
    //     return new DefaultPointcutAdvisor(pointcut, transactionInterceptor());
    // }
    @Bean
    public TransactionAttributeSource transactionAttributeSource() {
        return new AnnotationTransactionAttributeSource();
    }

    @Bean
    public TransactionInterceptor transactionInterceptor() {
        // TransactionInterceptor interceptor = new TransactionInterceptor();
        // interceptor.setTransactionAttributeSource(transactionAttributeSource());
        // interceptor.setTransactionManager(transactionManager());
        // return interceptor;
        return null;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}
